package c_leetcode.a_88_merge_sorted_array;

/**
 * 双指针
 * 时间复杂度    O(m+n)
 * 空间复杂度    O(m)
 * ------
 * 思路：双指针取两个数组间的最小值
 *
 * @author FRSF
 * @since 2025/2/26 21:46
 */
public class DoublePoint {
    public static void main(String[] args) {
        new DoublePoint()
                .merge(new int[]{1, 2, 3, 0, 0, 0}, 3, new int[]{2, 5, 6}, 3);
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        // nums1拷贝
        int[] copy1 = new int[m];
        System.arraycopy(nums1, 0, copy1, 0, m);

        // 双指针遍历
        int p = 0, p1 = 0, p2 = 0;
        while ((p1 < m) && (p2 < n)) {
            nums1[p++] = copy1[p1] < nums2[p2] ? copy1[p1++] : nums2[p2++];
        }

        // 剩余元素拷贝
        while (p1 < m) {
            nums1[p++] = copy1[p1++];
        }
        while (p2 < n) {
            nums1[p++] = nums2[p2++];
        }
    }
}
